package com.dcbeida.activity.inventory;


import android.app.Activity;
import android.content.DialogInterface;
import android.support.annotation.Nullable;

import com.dcbeida.MyApplication;
import com.dcbeida.bean.Drug;
import com.dcbeida.bean.DrugBeanWithPand;
import com.dcbeida.bean.DrugInfo;
import com.dcbeida.bean.DrugInfoList;
import com.dcbeida.bean.DrugListBean;
import com.dcbeida.bean.Result_Api;
import com.dcbeida.callback.DialogCallback;
import com.dcbeida.model.InventoryModel;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by hp on 2016/5/12.
 */
public class InventoryPresenter implements InventoryContract.Present {


    private InventoryContract.View view;

    private InventoryModel model;

    private Activity act;

    private List<DrugInfo> drugInfoList;

    private Drug drug;


    //删除的标志位
    private int deletePos;

    public InventoryPresenter(InventoryContract.View view, Activity act) {
        //我这里直接把activity作为view，所以不需要
        //mView.setPresenter(this);
        this.view = view;
        model = InventoryModel.newInstance();
        drugInfoList = new ArrayList<>();
        this.act = act;

    }

    @Override
    public void getAllInventoryCount() {
        model.getCountDate(new CountCallBack(act, Drug.class, lis));
    }

    @Override
    public void getdrugcodeinfo(String drugcode) {
        model.getDrugDetailDate(drugcode, new GetdrugcodeCallBack(act, DrugInfo.class, lis));
    }

    @Override
    public void addinventorydrugcodes(String drugcode) {
        model.addinventorydrugcodes(drugcode, new AddinventorydrugcodesCallBack(act, DrugInfoList.class, lis));
    }

    @Override
    public void addInventory() {
//        if (drug == null) {
//            view.showTip("没有查询到药品");
//            return;
//        }

//        DrugInfo paramDrug = drugInfoList.get(0);
//
//        deletePos = 0;
//        for (int i = 1; i < drugInfoList.size(); i++) {
//            if (drugInfoList.get(i).getDrugid() != paramDrug.getDrugid())
//                break;
//            else {
//                deletePos = i;
//            }
//        }


        model.addInventory(0, 0, new ApiCallBack(act, Result_Api.class, lis));
    }

    @Override
    public boolean DrugIsEmpty() {
        return drugInfoList.size() == 0;
    }


    @Override
    public void start() {

    }


    //取消dialog 取消请求
    DialogInterface.OnCancelListener lis = new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            model.getCancel();
        }
    };


    //数量返回
    class CountCallBack<T> extends DialogCallback<T> {

        public CountCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                if (data instanceof Drug) {
                    drug = (Drug) data;
                    view.setCount(((Drug) data).getStock());
                }
            }
        }


        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip("数据未请求成功,请检查网络...");


        }
    }

    int quantity;

    //根据条形码查询详细信息
    class GetdrugcodeCallBack<T> extends DialogCallback<T> {

        public GetdrugcodeCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                if (data instanceof DrugInfo) {
                    if (drug != null && drug.getDrugid() == ((DrugInfo) data).getDrugid()) {
                        if (drugInfoList.contains(data)) {
                            view.showTip("此款药品已存在列表中...");
                            return;
                        }
                        quantity += ((DrugInfo) data).getQuantity();
                        drugInfoList.add((DrugInfo) data);
                        view.addItem((DrugInfo) data);
                        view.setQuantity(drugInfoList.size(), quantity);
                    } else {
                        view.showTip("此款药品不统计在该款药品ID下");
                    }
                }
            } else {
                view.showTip("没有查询到此药品");
            }
        }


        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip("数据未请求成功,请检查网络...");


        }
    }


    //根据条形码查询详细信息
    class AddinventorydrugcodesCallBack<T> extends DialogCallback<T> {

        public AddinventorydrugcodesCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                if (data instanceof DrugInfoList) {


                    if (((DrugInfoList) data).getList().size() > 0) {
                        DrugInfo info = ((DrugInfoList) data).getList().get(0);
                        if (drugInfoList.contains(info)) {
                            view.showTip("此款药品已存在列表中...");
                            return;
                        }

                        DrugListBean drugListBean = MyApplication.getInstance().getmDrugBean();
                        if (drugListBean != null && drugListBean.getList() != null && !drugListBean.getList().isEmpty()) {
                            boolean isExit = false;//是否存在在这个列表中
                            for (Drug drug : drugListBean.getList()) {
                                if (drug.getDrugid() == info.getDrugid()) {
                                    info.setStock(drug.getStock());
                                    isExit = true;
                                }
                            }
                            if (!isExit) {
                                view.showTip("药品码错误");
                                return;
                            }
                        }

                        int pos = -1;
                        int head_pos = 0;
                        for (int i = 0; i < drugInfoList.size(); i++) {
                            if (drugInfoList.get(i).getDrugid() == (info).getDrugid()) {
                                if (drugInfoList.get(i).isFirst()) {
                                    head_pos = i;
                                }
                                pos = i;
                            }
                        }

                        if (pos == -1) {
                            info.setFirst(true);
                            info.setCount(info.getQuantity());
                            pos = drugInfoList.size();
                            head_pos = pos;
                        } else {
                            info.setFirst(false);
                            DrugInfo headInfo = drugInfoList.get(head_pos);
                            int count = headInfo.getCount() + info.getQuantity();
                            headInfo.setCount(count);
                            pos = pos + 1;

                        }
                        drugInfoList.add(pos, info);
                        view.addItem(pos, info, head_pos, drugInfoList.get(head_pos));
                        //view.setQuantity(drugInfoList.size(), quantity);
                    } else {
                        view.showTip("没有查询到此药品");
                    }

                }
            } else {
                view.showTip("没有查询到此药品");
            }
        }


        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip("数据未请求成功,请检查网络...");


        }
    }


    //提交盘点请求服务器返回结果
    class ApiCallBack<T> extends DialogCallback<T> {

        public ApiCallBack(Activity activity, Class<T> tclass, DialogInterface.OnCancelListener listener) {
            super(activity, tclass, listener);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                if (((Result_Api) data).getCode() == 1) {
                    view.showTip(((Result_Api) data).getMsg());
                    view.confimSuccess();

//                    drugInfoList.subList(0, deletePos + 1).clear();
//                    if (drugInfoList.size() > 0) {
//                        addInventory();
//                    } else {
//                        view.confimSuccess();
//                    }
                }
            }
        }


        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip("数据未请求成功,请检查网络...");


        }
    }

}
